/*     */ package com.qmsk.session;
/*     */ 
/*     */ import com.qmsk.business.manage.userOperLog.dao.UserOperLogDao;
/*     */ import com.qmsk.business.shopList.ShopMap;
/*     */ import com.qmsk.session.bean.CSessionUser;
/*     */ import com.utils.http.UrlUtils;
/*     */ import com.utils.jdbc.connection.DbConnection;
/*     */ import com.utils.web.ResponseUtils;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.sql.SQLException;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.servlet.Filter;
/*     */ import javax.servlet.FilterChain;
/*     */ import javax.servlet.FilterConfig;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.ServletRequest;
/*     */ import javax.servlet.ServletResponse;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.apache.commons.lang.StringUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.springframework.context.ApplicationContext;
/*     */ import org.springframework.web.context.support.WebApplicationContextUtils;
/*     */ 
/*     */ public class UserOnLineFilter extends HttpServlet
/*     */   implements Filter
/*     */ {
/*  39 */   private static final Logger logger = Logger.getLogger(UserOnLineFilter.class);
/*     */   private static final long serialVersionUID = 1L;
/*  42 */   private static final URLDoWhiteList urlDoWhiteList = new URLDoWhiteList();
/*     */ 
/*  44 */   private static final Pattern BUSS_TYPE = Pattern.compile("^/[\\w]+?/([\\w]+?)/([\\w]+?)/[\\S]+?$");
/*     */ 
/*     */   public void init(FilterConfig config) throws ServletException
/*     */   {
/*  48 */     String whiteListURLStr = config.getInitParameter("whiteListURL");
/*  49 */     urlDoWhiteList.init(whiteListURLStr);
/*     */   }
/*     */ 
/*     */   public String getIpAddr(HttpServletRequest request) {
/*  53 */     String ip = request.getHeader("x-forwarded-for");
/*  54 */     if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
/*  55 */       ip = request.getHeader("Proxy-Client-IP");
/*     */     }
/*  57 */     if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
/*  58 */       ip = request.getHeader("WL-Proxy-Client-IP");
/*     */     }
/*  60 */     if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
/*  61 */       ip = request.getRemoteAddr();
/*     */     }
/*  63 */     return ip;
/*     */   }
/*     */ 
/*     */   public static ApplicationContext getApplicationContextOfRequest(HttpServletRequest request)
/*     */   {
/*  69 */     return 
/*  70 */       WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession()
/*  71 */       .getServletContext());
/*     */   }
/*     */ 
/*     */   private Boolean writeToLog(HttpServletRequest request, String funCode, String funOperCode, String shopId)
/*     */   {
/*  80 */     String sIP = getIpAddr(request);
/*  81 */     Boolean bPower = Boolean.valueOf(false);
/*  82 */     CSessionUser cs = UserSessionInfo.user_getUserOfRequest(request);
/*  83 */     Long userId = null;
/*  84 */     String userName = null;
/*  85 */     if (cs != null) {
/*  86 */       userId = Long.valueOf(cs.getUserId());
/*  87 */       userName = cs.getUserName();
/*     */     } else {
/*  89 */       userId = Long.valueOf(0L);
/*  90 */       userName = "非法身份";
/*     */     }
/*     */ 
/*  95 */     ApplicationContext context = getApplicationContextOfRequest(request);
/*  96 */     UserOperLogDao d = (UserOperLogDao)context.getBean("UserOperLogDao");
/*     */     try {
/*  98 */       if (d != null)
/*     */       {
/* 102 */         bPower = Boolean.valueOf(true);
/*     */ 
/* 104 */         Integer nRst = d.insertUserLog(userId, userName, funCode, 
/* 105 */           funOperCode, bPower, sIP, shopId);
/* 106 */         if (nRst.intValue() > 0)
/* 107 */           DbConnection.commit();
/*     */         else
/* 109 */           DbConnection.rollback();
/*     */       }
/*     */     }
/*     */     catch (SQLException e) {
/* 113 */       DbConnection.rollback();
/* 114 */       logger.error(e);
/*     */     }
/* 116 */     return bPower;
/*     */   }
/*     */ 
/*     */   private Integer isRequestLogin(HttpServletRequest request)
/*     */   {
/* 123 */     Integer nReturn = Integer.valueOf(-1);
/* 124 */     String currentURL = request.getRequestURI();
/*     */ 
/* 126 */     int nPos1 = currentURL.indexOf("/", 1);
/* 127 */     String functionCode = "";
/* 128 */     String functionOperCode = "";
/* 129 */     String targetUrlRight = "";
/* 130 */     String shopId = "";
/*     */ 
/* 132 */     String currUrl = request.getRequestURI();
/* 133 */     Matcher m = BUSS_TYPE.matcher(currUrl);
/* 134 */     String targetURL = null;
/* 135 */     if (m.find()) {
/* 136 */       targetURL = m.group(1);
/*     */     }
/*     */ 
/* 140 */     if (nPos1 >= 0) {
/* 141 */       int nPos2 = currentURL.indexOf("/", nPos1 + 1);
/* 142 */       if (nPos2 >= 0) {
/* 143 */         functionCode = currentURL.substring(nPos1 + 1, nPos2);
/* 144 */         int nPos3 = currentURL.indexOf("/", nPos2 + 1);
/* 145 */         if (nPos3 >= 0) {
/* 146 */           functionOperCode = currentURL.substring(nPos2 + 1, nPos3);
/* 147 */           int nPos4 = currentURL.indexOf("/", nPos3 + 1);
/* 148 */           if (nPos4 >= 0) {
/* 149 */             String sSubShopId = currentURL.substring(nPos3 + 1, 
/* 150 */               nPos4);
/* 151 */             int nPos5 = currentURL.indexOf("/", nPos4 + 1);
/* 152 */             if ((nPos5 < 0) && 
/* 154 */               (StringUtils.equalsIgnoreCase(sSubShopId, 
/* 154 */               "shopId"))) {
/* 155 */               shopId = currentURL.substring(nPos4 + 1, 
/* 156 */                 currentURL.length());
/*     */             }
/* 159 */             else if (StringUtils.equalsIgnoreCase(sSubShopId, 
/* 159 */               "shopId")) {
/* 160 */               shopId = currentURL.substring(nPos4 + 1, nPos5);
/*     */             }
/*     */           }
/*     */         }
/*     */ 
/* 165 */         targetUrlRight = currentURL.substring(nPos1, 
/* 166 */           currentURL.length());
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 172 */     if (NoAuth.isTargetAuth(targetURL)) {
/* 173 */       System.err.println("pass");
/* 174 */       return null;
/*     */     }
/*     */ 
/* 181 */     HttpSession session = request.getSession(false);
/* 182 */     if (session != null)
/* 183 */       logger.info("sessionId: " + session.getId());
/*     */     else {
/* 185 */       logger.info("sessionId: null");
/*     */     }
/* 187 */     logger.info(request.getMethod() + " --> " + targetUrlRight);
/*     */ 
/* 189 */     if (("userlg".compareToIgnoreCase(functionCode) == 0) || 
/* 190 */       ("html".compareToIgnoreCase(functionCode) == 0)) {
/* 191 */       nReturn = null;
/*     */     }
/*     */     else
/*     */     {
/* 195 */       Boolean nPower = writeToLog(request, functionCode, 
/* 196 */         functionOperCode, shopId);
/* 197 */       if ((session == null) || 
/* 198 */         (!(UserSessionInfo.user_isLoginOfHttpSession(session)))) {
/* 199 */         nReturn = Integer.valueOf(-1);
/*     */       }
/* 201 */       else if (UserSessionInfo.user_isDoubleOnLineOfHttpSession(session))
/* 202 */         nReturn = Integer.valueOf(1);
/*     */       else {
/* 204 */         nReturn = (nPower.booleanValue()) ? null : Integer.valueOf(0);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 211 */     nReturn = null;
/* 212 */     return nReturn;
/*     */   }
/*     */ 
/*     */   private Short preRedirectUrl(UrlUtils u, HttpServletRequest request, HttpServletResponse response)
/*     */     throws Exception
/*     */   {
/* 226 */     Short nReturn = null;
/*     */ 
/* 228 */     String currentURL = request.getRequestURI();
/*     */ 
/* 230 */     String[] arUrl = currentURL.split("/");
/* 231 */     if ((arUrl != null) && (arUrl.length > 0)) {
/* 232 */       if (StringUtils.equals(arUrl[(arUrl.length - 1)], "pt")) {
/* 233 */         nReturn = Short.valueOf((short) 2);
/* 234 */         for (int i = 0; i < arUrl.length - 1; ++i) {
/* 235 */           if (StringUtils.equals(arUrl[i], "shopId")) {
/* 236 */             Long shopId_outId = Long.valueOf(arUrl[(i + 1)]);
/* 237 */             Long shopId = ShopMap.getShopIdOfOutId(shopId_outId);
/* 238 */             if (shopId == null) {
/* 239 */               ResponseUtils.setErrorMsgResponse("shopId为空", 
/* 240 */                 response);
/* 241 */               break;
/*     */             }
/* 243 */             arUrl[(i + 1)] = String.valueOf(shopId);
/*     */           }
/* 245 */           u.add(arUrl[i]);
/*     */         }
/* 247 */         u.toUrl();
/*     */       } else {
/* 249 */         nReturn = Short.valueOf((short) 1);
/*     */       }
/*     */     }
/* 252 */     return nReturn;
/*     */   }
/*     */ 
/*     */   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
/*     */     throws IOException, ServletException
/*     */   {
/* 258 */     HttpServletRequest request = (HttpServletRequest)servletRequest;
/* 259 */     HttpServletResponse response = (HttpServletResponse)servletResponse;
/*     */ 
/* 264 */     Integer nReturn = isRequestLogin(request);
/* 265 */     if (nReturn == null) {
/* 266 */       UrlUtils u = new UrlUtils();
/* 267 */       Short sUrlReturn = null;
/*     */       try {
/* 269 */         sUrlReturn = preRedirectUrl(u, request, response);
/* 270 */         if (sUrlReturn != null) {
/* 271 */           if (sUrlReturn.shortValue() == 1) {
/* 272 */             filterChain.doFilter(request, response);
/* 273 */           } else if (sUrlReturn.shortValue() == 2) {
/* 274 */             response.setStatus(307);
/* 275 */             response.setHeader("location", u.toUrl());
/*     */           }
/*     */         }
/*     */ 
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 282 */         logger.error(e.getMessage());
/*     */       }
/* 284 */     } else if (nReturn.intValue() == -1)
/*     */     {
/* 286 */       ResponseUtils.setResponseHeadNotLogin((HttpServletResponse)servletResponse, "not login");
/* 287 */     } else if (nReturn.intValue() == 0)
/*     */     {
/* 289 */       ResponseUtils.setResponseHeadNotPower((HttpServletResponse)servletResponse, "no power"); } else {
/* 290 */       if (nReturn.intValue() != 1)
/*     */         return;
/* 292 */       ResponseUtils.setResponseHeadNotOnLine((HttpServletResponse)servletResponse, "not online");
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\新建文件夹\WEB-INF\classes\
 * Qualified Name:     com.qmsk.session.UserOnLineFilter
 * JD-Core Version:    0.5.3
 */